﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using System.Collections;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Controls;

namespace CIIS.Common
{
    public class DevControlUtils
    {
        public static readonly DevControlUtils Instance = new DevControlUtils();
        DevControlUtils() { }

        /// <summary>
        /// 启用gridcontrol的导航条
        /// </summary>
        /// <param name="gc"></param>
        /// <param name="enableFlags"></param>
        public void EnableGridControlNavgator(GridControl gc, ENavigatorButton enableFlags)
        {
            gc.UseEmbeddedNavigator = true;
            gc.EmbeddedNavigator.Buttons.Append.Visible = ((enableFlags & ENavigatorButton.Append) == ENavigatorButton.Append);
            gc.EmbeddedNavigator.Buttons.CancelEdit.Visible = ((enableFlags & ENavigatorButton.CancelEdit) == ENavigatorButton.CancelEdit);
            gc.EmbeddedNavigator.Buttons.Edit.Visible = ((enableFlags & ENavigatorButton.Edit) == ENavigatorButton.Edit);
            gc.EmbeddedNavigator.Buttons.EndEdit.Visible = ((enableFlags & ENavigatorButton.EndEdit) == ENavigatorButton.EndEdit);
            gc.EmbeddedNavigator.Buttons.First.Visible = ((enableFlags & ENavigatorButton.First) == ENavigatorButton.First);
            gc.EmbeddedNavigator.Buttons.Last.Visible = ((enableFlags & ENavigatorButton.Last) == ENavigatorButton.Last);
            gc.EmbeddedNavigator.Buttons.Next.Visible = ((enableFlags & ENavigatorButton.Next) == ENavigatorButton.Next);
            gc.EmbeddedNavigator.Buttons.NextPage.Visible = ((enableFlags & ENavigatorButton.NextPage) == ENavigatorButton.NextPage);
            gc.EmbeddedNavigator.Buttons.Prev.Visible = ((enableFlags & ENavigatorButton.Prev) == ENavigatorButton.Prev);
            gc.EmbeddedNavigator.Buttons.PrevPage.Visible = ((enableFlags & ENavigatorButton.PrevPage) == ENavigatorButton.PrevPage);
            gc.EmbeddedNavigator.Buttons.Remove.Visible = ((enableFlags & ENavigatorButton.Remove) == ENavigatorButton.Remove);
        }

        [Flags]
        public enum ENavigatorButton : int
        {
            Append,
            CancelEdit,
            Edit,
            EndEdit,
            First,
            Last,
            Next,
            NextPage,
            Prev,
            PrevPage,
            Remove
        }

        /// <summary>
        /// 初始化gridview，进行简单配置
        /// </summary>
        /// <param name="showGroupPanel"></param>
        public void InitGridView(GridView gv, bool showGroupPanel)
        {
            gv.OptionsView.ShowGroupPanel = showGroupPanel;
        }

        /// <summary>
        /// 初始化gridview的列头配置
        /// </summary>
        public void InitGridViewColumnHeader()
        {
        }

        /// <summary>
        /// 绑定RepositoryItemSearchLookUpEdit控件的常用内容
        /// </summary>
        /// <param name="edit"></param>
        /// <param name="data"></param>
        /// <param name="displayMember"></param>
        /// <param name="valueMember"></param>
        public void InitRepositoryItemSearchLookUpEdit(RepositoryItemSearchLookUpEdit edit, DropdownControlParam dcp)
        {
            if (dcp != null)
            {
                InitRepositoryItemSearchLookUpEdit(edit, dcp.DataSource, dcp.DisplayMember, dcp.ValueMember, dcp.Caption, dcp.NullText);
            }
        }
        public void InitRepositoryItemSearchLookUpEdit(RepositoryItemSearchLookUpEdit edit, IList data, string displayMember, string valueMember, string caption, string nullText)
        {
            if (edit == null || data == null || string.IsNullOrWhiteSpace(displayMember) || string.IsNullOrWhiteSpace(valueMember))
            {
                return;
            }
            edit.NullText = nullText;
            edit.DataSource = data;
            edit.DisplayMember = displayMember;
            edit.ValueMember = valueMember;
            edit.View.Columns.Clear();
            edit.View.Columns.AddVisible(displayMember, caption);
        }
        /// <summary>
        /// 绑定SearchLookUpEdit控件常用内容
        /// </summary>
        /// <param name="edit"></param>
        /// <param name="data"></param>
        /// <param name="displayMember"></param>
        /// <param name="valueMember"></param>
        /// <param name="caption"></param>
        public void InitSearchLookUpEdit(SearchLookUpEdit edit, DropdownControlParam dcp)
        {
            if (dcp != null)
            {
                InitSearchLookUpEdit(edit, dcp.DataSource, dcp.DisplayMember, dcp.ValueMember, dcp.Caption, dcp.NullText);
            }
        }
        public void InitSearchLookUpEdit(SearchLookUpEdit edit, IList data, string displayMember, string valueMember, string caption, string nullText)
        {
            if (edit == null)
            {
                return;
            }
            InitRepositoryItemSearchLookUpEdit(edit.Properties, data, displayMember, valueMember, caption, nullText);
        }


        /// <summary>
        /// 绑定RepositoryItemLookUpEdit控件的常用内容
        /// </summary>
        /// <param name="edit"></param>
        /// <param name="data"></param>
        /// <param name="displayMember"></param>
        /// <param name="valueMember"></param>
        public void InitRepositoryItemLookUpEdit(RepositoryItemLookUpEdit edit, DropdownControlParam dcp)
        {
            if (dcp != null)
            {
                InitRepositoryItemLookUpEdit(edit, dcp.DataSource, dcp.DisplayMember, dcp.ValueMember, dcp.Caption, dcp.NullText);
            }
        }
        public void InitRepositoryItemLookUpEdit(RepositoryItemLookUpEdit edit, IList data, string displayMember, string valueMember, string caption, string nullText)
        {
            if (edit == null || data == null || string.IsNullOrWhiteSpace(displayMember) || string.IsNullOrWhiteSpace(valueMember))
            {
                return;
            }
            edit.DataSource = data;
            edit.DisplayMember = displayMember;
            edit.ValueMember = valueMember;
            edit.Columns.Add(new LookUpColumnInfo(displayMember, caption));
            edit.NullText = nullText;
        }
        public void InitRepositoryItemLookUpEditByEnum(RepositoryItemLookUpEdit edit, Type enumType)
        {
            DropdownControlParam dcp = Utils.Instance.GetDcpFromEnums(enumType);
            InitRepositoryItemLookUpEdit(edit, dcp);
        }

        /// <summary>
        /// 绑定LookUpEdit控件的常用内容
        /// </summary>
        /// <param name="edit"></param>
        /// <param name="data"></param>
        /// <param name="displayMember"></param>
        /// <param name="valueMember"></param>
        public void InitLookUpEdit(LookUpEdit edit, DropdownControlParam dcp)
        {
            if (dcp != null)
            {
                InitLookUpEdit(edit, dcp.DataSource, dcp.DisplayMember, dcp.ValueMember, dcp.Caption, dcp.NullText);
            }
        }
        public void InitLookUpEdit(LookUpEdit edit, IList data, string displayMember, string valueMember, string caption, string nullText)
        {
            if (edit == null)
            {
                return;
            }
            InitRepositoryItemLookUpEdit(edit.Properties, data, displayMember, valueMember, caption, nullText);
        }
        public void InitLookUpEditByEnum(LookUpEdit edit, Type enumType)
        {
            DropdownControlParam dcp = Utils.Instance.GetDcpFromEnums(enumType);
            InitLookUpEdit(edit, dcp);
        }

        /// <summary>
        /// 绑定CheckedComboEdit控件的常用内容
        /// </summary>
        /// <param name="edit"></param>
        /// <param name="data"></param>
        /// <param name="displayMember"></param>
        /// <param name="valueMember"></param>
        public void InitCheckedComboEdit(CheckedComboBoxEdit edit, IList data, string displayMember, string valueMember)
        {
            if (edit == null || data == null || string.IsNullOrWhiteSpace(displayMember) || string.IsNullOrWhiteSpace(valueMember))
            {
                return;
            }
            edit.Properties.DataSource = data;
            edit.Properties.DisplayMember = displayMember;
            edit.Properties.ValueMember = valueMember;
            edit.Properties.NullText = string.Empty;
        }
    }
}
